package lab03.test09;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
//错误：两类：1.编译错误  IDEA会出现红色标记
//2.运行时错误，编写时没有飘红，要调试
public class Client {
    public static void main(String[] args) {
        //红点为断点
        //鼠标放在变量上，看变量值与预期值是否一致
        List<Book> books = new ArrayList<Book>();//
        // Book这个类的list 定义为books

            String path = "F://book.txt";//找到储存的文本
            String path1 = "F://book1.txt";//找到储存的文本
            BufferedReader reader = null;//读文件
            BufferedWriter writer  = null;
        try {
                reader = new BufferedReader(new FileReader(path));//读文件book.txt
            //字符流按行去读，放在list里面
                String line = null;//定义line
                while((line = reader.readLine()) != null){//循环读,读到list集合中
                    String[] tokens  = line.split("\t");
                    Book book = new Book();
                book.bookNo = Integer.parseInt(tokens[0]);
                book.bookName = tokens[1];
                book.version= tokens[2];
                book.price = Double.parseDouble(tokens[3]);
                book.sales = Double.parseDouble(tokens[4]);

                books.add(book);

            }
            //修改
            int i = 1;
            for (Book b : books){
              if (i==3){
                  b.price = b.price +20;
              }
              if (i==4){
                  b.price = b.price +30;
              }
              i++;
            }
            //修改后写到文件里
            //单步调试
            writer = new BufferedWriter(new FileWriter(path));
            for (Book b: books){
                writer.write(b.bookName+"  "
                        +b.bookNo +"   "+
                        b.version+"   "
                        +b.price+"  "+
                        b.sales);
                writer.newLine();
            }


            } catch (IOException e) {
                e.printStackTrace();
            }finally {

            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
        }


